using System;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace Nextensions.GUI
{
	public static class TextBoxLinker
	{
		private static void HandleLinks(TextBox Textbox)
		{
			Textbox.MouseMove += new MouseEventHandler(Textbox_MouseMove);
		}

		private static void Textbox_MouseMove(object sender, MouseEventArgs e)
		{
			TextBox tb = (TextBox)sender;

			if (sender == null)
			{
				return;
			}

			string link = LinkFromText(tb.Text, tb.GetCharIndexFromPosition(e.Location));

			if (link == null)
			{
				return;
			}

			HandleLink(link);
		}

		private static string LinkFromText(string text, int caretPosition)
		{
			string quotesMatchLeft = @"""file:///([^""]+)$";
			string quotesMatchRight = @"^([^""]+)""";
			string left, right;

			left = Regex.Match(text.Substring(0, caretPosition), quotesMatchLeft).Groups[1].Value;

			if(left=="")
				return null;

			right = Regex.Match(text.Substring(caretPosition), quotesMatchRight).Groups[1].Value;

//			Console.WriteLine("Quotes match\r\n\tLeft: {0}\r\n\tRight: {1}" , Regex.Replace(left , @"\n" , "" , RegexOptions.Multiline) , Regex.Replace(right, @"\n" , "" , RegexOptions.Multiline));

			return left + right;
		}

		private static void HandleLink(string link)
		{
			throw new NotImplementedException();
		}
	}
}